﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _8280WEBSocket
{
    public partial class frmNodeForm : Form
    {

        /// <summary>
        /// 主窗体访问接口
        /// </summary>
        protected INMain mMainForm;

        /// <summary>
        /// 在UI线程上执行一个方法
        /// </summary>
        /// <param name="p"></param>
        protected void Invoke(Action p)
        {
            Invoke((Delegate)p);
        }

        /// <summary>
        /// 线程休眠
        /// </summary>
        /// <param name="time"></param>
        private void Sleep(int time)
        {
            System.Threading.Thread.Sleep(time);
        }
        public frmNodeForm()
        {
            InitializeComponent();
        }
        public frmNodeForm(INMain main)
        {
            InitializeComponent();
            mMainForm = main;
        }
        public Control FindControl(Control parentControl, string findCtrlName)
        {
            Control _findedControl = null;
            if (!string.IsNullOrEmpty(findCtrlName) && parentControl != null)
            {
                foreach (Control ctrl in parentControl.Controls)
                {
                    if (ctrl.Name.Equals(findCtrlName))
                    {
                        _findedControl = ctrl;
                        break;
                    }
                }
            }
            return _findedControl;
        }

        public void SetAllTimePicker(Control parent, string beginControlName, string endControlName, string week)
        {
            if (week == null)
            {
                week = string.Empty;
            }
            var group = week.Split(',');
            for (int i = 0; i < 8; i++)
            {
                var index = i + 1;
                DateTimePicker beginTimePicker = FindControl(parent, beginControlName + index) as DateTimePicker;
                DateTimePicker endTimePicker = FindControl(parent, endControlName + index) as DateTimePicker;
                string[] weeks;
                if (group.Length <= i)
                {
                    weeks = new string[] { "" };
                }
                else
                {
                    weeks = group[i].Split('-');
                }
                if (weeks.Length == 2)
                {
                    beginTimePicker.Value = DateTime.Parse("2021-11-25 " + weeks[0] + ":00");
                    endTimePicker.Value = DateTime.Parse("2021-11-25 " + weeks[1] + ":00");
                }
                else
                {
                    beginTimePicker.Value = DateTime.Parse("2021-11-25 00:00:00");
                    endTimePicker.Value = DateTime.Parse("2021-11-25 00:00:00");
                }
            }

        }

        public string GetAllTimePicKer(Control parent, string beginControlName, string endControlName)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 8; i++)
            {
                DateTimePicker beginTimePicker = FindControl(parent, beginControlName + (i + 1).ToString()) as DateTimePicker;
                DateTimePicker endTimePicker = FindControl(parent, endControlName + (i + 1).ToString()) as DateTimePicker;
                builder.Append(beginTimePicker.Value.ToString("HH:mm"))
                    .Append("-")
                    .Append(endTimePicker.Value.ToString("HH:mm"))
                    .Append(",");

            }
            builder.Length--;
            return builder.ToString();
        }



        public static string ConvertJsonString(string str)
        {
            //格式化json字符串
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

    }
}
